package cn.ecnu.service.impl;

import cn.ecnu.mapper.JournalMapper;
import cn.ecnu.pojo.Journal;
import cn.ecnu.service.JournalService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class JournalServiceImpl extends ServiceImpl<JournalMapper, Journal> implements JournalService {
    @Override
    public List<Journal> getTop10JournalsByViews() {
        QueryWrapper<Journal> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("views");
        queryWrapper.last("LIMIT 10");
        return list(queryWrapper);
    }

    @Override
    public List<Journal> getTop10JournalsByFollows() {
        QueryWrapper<Journal> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("follows");
        queryWrapper.last("LIMIT 10");
        return list(queryWrapper);
    }
    @Override
    public void incrementViews(Long id) {
        Journal journal = getById(id);
        if (journal != null) {
            journal.setViews(journal.getViews() + 1);
            updateById(journal);
        }
    }

    @Override
    public void incrementFollows(Long id) {
        Journal journal = getById(id);
        if (journal != null) {
            journal.setFollows(journal.getFollows() + 1);
            updateById(journal);
        }
    }

    @Override
    public void decrementFollows(Long id) {
        Journal journal = getById(id);
        if (journal != null) {
            journal.setFollows(journal.getFollows() - 1);
            updateById(journal);
        }
    }
}
